home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / MuManual / C_Sources / MuFastChip.c < prev    next >
C/C++ Source or Header  |  1999-11-28  |  14KB  |  367 lines

  1. /*****************************************************************
  2.  ** MuFastChip                                                  **
  3.  **                                                             **
  4.  ** Enable imprecise/nonserial flags for Chip memory            **
  5.  ** Release 40.2, © 1999 THOR-Software inc.                     **
  6.  ** 28.11.1999 Thomas Richter                                   **
  7.  *****************************************************************/
  8.  
  9. /// Includes
  10. #include <exec/types.h>
  11. #include <exec/memory.h>
  12. #include <exec/ports.h>
  13. #include <exec/execbase.h>
  14. #include <dos/dos.h>
  15. #include <mmu/mmubase.h>
  16. #include <mmu/context.h>
  17. #include <mmu/mmutags.h>
  18. #include <workbench/startup.h>
  19.  
  20. #include <thor/conversions.h>
  21.  
  22. #include <proto/exec.h>
  23. #include <proto/mmu.h>
  24. #include <proto/dos.h>
  25. #include <proto/icon.h>
  26. #include <string.h>
  27. ///
  28. /// Defines
  29. #define STRINGDATE "28.11.99"
  30. #define STRINGVERSION "40.2"
  31. #define TEMPLATE "ON/S,OFF/S"
  32.  
  33. #define OPT_ON 0
  34. #define OPT_OFF 1
  35. #define OPT_WINDOW 2
  36. #define OPT_COUNT 3
  37. ///
  38. /// Statics
  39. struct MMUBase *MMUBase;
  40. struct DosLibrary *DOSBase;
  41. struct ExecBase *SysBase;
  42. struct Library *IconBase;
  43. ///
  44. /// Protos
  45. int __asm __saveds main(void);
  46. int SetCacheMode(ULONG from,ULONG size,ULONG flags,ULONG mask);
  47. struct RDArgs *ReadTTArgs(struct WBStartup *msg,LONG args[],struct RDArgs **tmp);
  48. int DisableFastChip(void);
  49. int EnableFastChip(void);
  50. BOOL FindChipLimits(ULONG *from,ULONG *size);
  51. ///
  52.  
  53. char version[]="$VER: MuFastChip " STRINGVERSION " (" STRINGDATE ") © THOR";
  54.  
  55. /// main
  56. int __asm __saveds main(void)
  57. {
  58. LONG args[OPT_COUNT];
  59. struct RDArgs *rd,*myrd;
  60. struct Process *proc;
  61. int rc=20;
  62. LONG err;
  63. struct WBStartup *msg;
  64. BPTR oldout;
  65. struct MsgPort *oldconsole;
  66.  
  67.         SysBase=*((struct ExecBase **)(4L));
  68.  
  69.         memset(args,0,sizeof(LONG)*OPT_COUNT);
  70.  
  71.         /* Wait for the workbench startup, if any */
  72.         proc=(struct Process *)FindTask(NULL);
  73.  
  74.         if (!(proc->pr_CLI)) {
  75.                 WaitPort(&(proc->pr_MsgPort));
  76.                 msg=(struct WBStartup *)GetMsg(&(proc->pr_MsgPort));
  77.         } else  msg=NULL;
  78.  
  79.         if (DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37L)) {
  80.                 if (MMUBase=(struct MMUBase *)OpenLibrary("mmu.library",41L)) {
  81.  
  82.                         err=ERROR_REQUIRED_ARG_MISSING;
  83.  
  84.                         myrd=NULL;      /* reset the temporary ReadArgs */
  85.                         oldout=NULL;
  86.                         oldconsole=NULL;
  87.                         if (msg) {
  88.                                 oldout=SelectOutput(NULL);
  89.                                 oldconsole=SetConsoleTask(NULL);
  90.                                 rd=ReadTTArgs(msg,args,&myrd);
  91.                         } else  rd=ReadArgs(TEMPLATE,args,NULL);
  92.  
  93.                         if (rd) {
  94.                                 if (!GetMMUType()) {
  95.                                         Printf("MuFastChip requires a working MMU.\n");
  96.                                         err=10;
  97.                                 } else {
  98.  
  99.                                         err=0;
  100.  
  101.                                         if (args[OPT_ON] || (!args[OPT_OFF])) {
  102.                                                 err=EnableFastChip();
  103.                                         }
  104.  
  105.                                         if (args[OPT_OFF]) {
  106.                                                 err=DisableFastChip();
  107.                                         }
  108.                                 }
  109.  
  110.                                 FreeArgs(rd);
  111.                                 if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  112.                                 if (msg)  Close(SelectOutput(NULL));
  113.                         } else  err=IoErr();
  114.  
  115.                         if (msg) {
  116.                                 SelectOutput(oldout);
  117.                                 SetConsoleTask(oldconsole);
  118.                         }
  119.  
  120.                         if (err<64) {
  121.                                 rc=err;
  122.                                 err=0;
  123.                         } else {
  124.                                 if (!msg) PrintFault(err,"MuFastChip failed");
  125.                                 rc=10;
  126.                         }
  127.                         SetIoErr(err);
  128.  
  129.                         CloseLibrary((struct Library *)MMUBase);
  130.                 } else if (!msg) PrintFault(ERROR_OBJECT_NOT_FOUND,"MuFastChip requires the mmu.library V41 or better");
  131.                 CloseLibrary((struct Library *)DOSBase);
  132.         }
  133.  
  134.         if (msg) {
  135.                 Forbid();
  136.                 ReplyMsg((struct Message *)msg);
  137.         }
  138.  
  139.         return rc;
  140. }
  141. ///
  142. /// ReadTTArgs
  143. struct RDArgs *ReadTTArgs(struct WBStartup *msg,LONG args[],struct RDArgs **tmp)
  144. {
  145. struct WBArg *wbarg;
  146. struct DiskObject *dop;
  147. char **tt;                      /* ToolTypes array */
  148. char *wbstr;                    /* Our self-made workbench argument string */
  149. char *here;
  150. BPTR oldlock;
  151. ULONG len;
  152. struct RDArgs *rd=NULL,*myrd=NULL;
  153. LONG err=0;
  154. BPTR newout;
  155.  
  156.         if (IconBase=OpenLibrary("icon.library",37L)) {
  157.                 if (wbarg=msg->sm_ArgList) {
  158.                         /* use a project icon if there is one... */
  159.                         if (msg->sm_NumArgs > 1) wbarg++;
  160.  
  161.                         /* go into the directory */
  162.                         oldlock=CurrentDir(wbarg->wa_Lock);
  163.  
  164.                         if (dop=GetDiskObject(wbarg->wa_Name)) {
  165.                                 if (tt=dop->do_ToolTypes) {
  166.                                         /* Read a special tool type for the output window */
  167.  
  168.                                         /* Calc the size of the argument string */
  169.  
  170.                                         len = 3;        /* reserve space for SPC,LF,NUL */
  171.                                         while (*tt) {
  172.                                                 len += strlen(*tt)+1;   /* string, plus space */
  173.                                                 tt++;
  174.                                         }
  175.  
  176.                                         if (wbstr=AllocVec(len,MEMF_PUBLIC)) {
  177.                                                 /* Now copy the arguments into this string, one by one
  178.                                                    and check whether the argument string is still valid. */
  179.  
  180.                                                 tt=dop->do_ToolTypes;
  181.                                                 here=wbstr;
  182.                                                 do{
  183.                                                         *here='\0';                     /* terminate string */
  184.                                                         /* Check whether this tool type is
  185.                                                            commented out. Just ignore it in this case */
  186.                                                         if (*tt) {
  187.                                                                 if (**tt=='(' || **tt==';')
  188.                                                                         continue;
  189.  
  190.                                                                 strcpy(here,*tt);      /* Add TT string */
  191.                                                         }
  192.                                                         len=strlen(here);
  193.                                                         here[len]='\n';
  194.                                                         here[len+1]='\0';               /* terminate string */
  195.  
  196.                                                         /* Now try to ReadArg' this string */
  197.  
  198.                                                         /* release old arguments left over from last loop */
  199.                                                         if (rd) FreeArgs(rd);
  200.                                                         if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  201.                                                         rd=NULL;
  202.                                                         memset(args,0,sizeof(LONG)*OPT_COUNT);
  203.  
  204.                                                         if (myrd=AllocDosObject(DOS_RDARGS,NULL)) {
  205.                                                                 /* Allocate and setup the ReadArgs source */
  206.                                                                 myrd->RDA_Source.CS_Buffer=wbstr;
  207.                                                                 myrd->RDA_Source.CS_Length=strlen(wbstr);
  208.  
  209.                                                                 if (rd=ReadArgs(TEMPLATE ",WINDOW/K",args,myrd)) {
  210.                                                                         /* Is this still valid? */
  211.                                                                         here[len]=' ';
  212.                                                                         here+=len+1;
  213.                                                                         /* if so, accept this argument and go on */
  214.                                                                 } else {
  215.                                                                         err=IoErr();
  216.                                                                         if (err==ERROR_NO_FREE_STORE) break;
  217.                                                                         else    err=0;  /* Ignore unknown or invalid arguments silently */
  218.                                                                 }
  219.                                                         } else {
  220.                                                                 err=ERROR_NO_FREE_STORE;
  221.                                                                 break;
  222.                                                         }
  223.                                                 }while(*tt++);
  224.  
  225.                                                 FreeVec(wbstr);
  226.                                         } else err=ERROR_NO_FREE_STORE;
  227.                                 } else err=ERROR_REQUIRED_ARG_MISSING; /* Huh, how should this happen ? */
  228.                                 FreeDiskObject(dop);
  229.                         } else err=IoErr();
  230.                         CurrentDir(oldlock);
  231.                 } else err=ERROR_REQUIRED_ARG_MISSING; /* This should not happen either */
  232.                 CloseLibrary(IconBase);
  233.         } else err=ERROR_OBJECT_NOT_FOUND;    /* This should not happen */
  234.  
  235.         /* Open an output stream */
  236.  
  237.         if (err==0) {
  238.                 if (newout=Open((args[OPT_WINDOW])?((char *)args[OPT_WINDOW]):("NIL:"),MODE_NEWFILE)) {
  239.                         SelectOutput(newout);
  240.                         /* Hack in the output console. Well, well... */
  241.                         SetConsoleTask(((struct FileHandle *)(BADDR(newout)))->fh_Type);
  242.                 } else err=IoErr();
  243.         }
  244.  
  245.         if (err) {
  246.                 if (rd)   FreeArgs(rd);
  247.                 if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  248.                 SetIoErr(err);
  249.                 rd=NULL;
  250.                 myrd=NULL;
  251.         }
  252.  
  253.         *tmp=myrd;
  254.         return rd;
  255. }
  256. ///
  257. /// EnableFastChip
  258. int EnableFastChip(void)
  259. {
  260. ULONG from,size;
  261.  
  262.         if (FindChipLimits(&from,&size)) {
  263.                 return SetCacheMode(from,size,MAPP_IMPRECISE|MAPP_NONSERIALIZED,MAPP_IMPRECISE|MAPP_NONSERIALIZED);
  264.         } else  return 20;
  265.  
  266. }
  267. ///
  268. /// DisableFastChip
  269. int DisableFastChip(void)
  270. {
  271. ULONG from,size;
  272.  
  273.         if (FindChipLimits(&from,&size)) {
  274.                 return SetCacheMode(from,size,0,MAPP_IMPRECISE|MAPP_NONSERIALIZED);
  275.         } else  return 20;
  276.  
  277. }
  278. ///
  279. /// FindChipLimits
  280. BOOL FindChipLimits(ULONG *from,ULONG *size)
  281. {
  282. struct MMUContext *ctx;
  283. ULONG psize;
  284. ULONG upper,lower;
  285. struct MemHeader *memh;
  286.  
  287.         ctx=DefaultContext();
  288.         psize=GetPageSize(ctx);
  289.         lower=0xffffffff;
  290.         upper=0;
  291.         Forbid();
  292.         for(memh=(struct MemHeader *)SysBase->MemList.lh_Head;
  293.             memh->mh_Node.ln_Succ;
  294.             memh=(struct MemHeader *)memh->mh_Node.ln_Succ) {
  295.  
  296.                 if (memh->mh_Attributes & MEMF_CHIP) {
  297.                         if ((ULONG)(memh->mh_Lower) < lower) {
  298.                                 lower=(ULONG)(memh->mh_Lower);
  299.                         }
  300.                         if ((ULONG)(memh->mh_Upper) > upper) {
  301.                                 upper=(ULONG)(memh->mh_Upper);
  302.                         }
  303.                 }
  304.         }
  305.         Permit();
  306.  
  307.         if (lower>=upper) {
  308.                 Printf("MuFastChip failed: no MEMF_CHIP memory found.\n");
  309.                 return FALSE;
  310.         }
  311.  
  312.         upper += psize-1;
  313.         lower &= -psize;
  314.         upper &= -psize;
  315.  
  316.  
  317.         *from=lower;
  318.         *size=upper-lower;
  319.  
  320.         return TRUE;
  321. }
  322. ///
  323. /// SetCacheMode
  324. int SetCacheMode(ULONG from,ULONG size,ULONG flags,ULONG mask)
  325. {
  326. struct MMUContext *ctx,*sctx;   /* default context, supervisorcontext */
  327. struct MinList *ctxl,*sctxl;
  328. int err;
  329.  
  330.         ctx=DefaultContext();   /* get the default context */
  331.         sctx=SuperContext(ctx); /* get the supervisor context for this one */
  332.  
  333.         LockContextList();
  334.         LockMMUContext(ctx);
  335.         LockMMUContext(sctx);
  336.  
  337.         err=ERROR_NO_FREE_STORE;
  338.  
  339.         if (ctxl=GetMapping(ctx)) {
  340.          if (sctxl=GetMapping(sctx)) {
  341.           if (SetProperties(ctx,flags,mask,from,size,TAG_DONE)) {
  342.            if (SetProperties(sctx,flags,mask,from,size,TAG_DONE)) {
  343.             if (RebuildTrees(ctx,sctx,NULL)) {
  344.                     err=0;
  345.             }
  346.            }
  347.           }
  348.  
  349.           if (err) {
  350.                   SetPropertyList(ctx,ctxl);
  351.                   SetPropertyList(sctx,sctxl);
  352.           }
  353.  
  354.          ReleaseMapping(sctx,sctxl);
  355.          }
  356.         ReleaseMapping(ctx,ctxl);
  357.         }
  358.  
  359.         UnlockMMUContext(sctx);
  360.         UnlockMMUContext(ctx);
  361.         UnlockContextList();
  362.  
  363.         return err;
  364. }
  365. ///
  366.  
  367.